package model;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;

public class EncrypterDisposition implements Encrypter {

	/* (non-Javadoc)
	 * @see model.Encrypter#encrypt(java.io.InputStream, java.io.OutputStream)
	 */
	public void encrypt(InputStream stream, OutputStream out) throws IOException {
		Charset utf8 = Charset.forName("UTF-8");
		Reader r = new InputStreamReader(stream, utf8);
		Writer w = new OutputStreamWriter(out, utf8);
		
		
		char[] helper = null;
		char[] secondhelper = null;
		int sign = r.read();
		for (int i = 0; sign >= 0; i+=10) {
			helper = new char[10];
			for(int j = 0; j < 10; j++) {
				if(sign >= 0) {
					helper[j] = (char) sign;
				} else {
					helper[j] = ' ';
				}
				sign = r.read();
			}
			if((i/10) %2 == 0) {
				secondhelper = helper;
				helper = null;
			}
			if(helper != null) {
				w.write(helper);
				w.write(secondhelper);
				w.flush();
			}
		}
		w.flush();
	}

	/* (non-Javadoc)
	 * @see model.Encrypter#decrypt(java.io.InputStream, java.io.OutputStream)
	 */
	public void decrypt(InputStream stream, OutputStream out) throws IOException {
		encrypt(stream, out);
	}
}
